package cn.kgc.spring01.converter;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/2/17
 * @Description:  日期类型转换器  Converter<S,T>  source  target
 */
public class DateConverter implements Converter<String, Date> {

    private String format;

    public void setFormat(String format) {
        this.format = format;
    }

    @Override
    public Date convert(String source) {  //  yyyy-MM-dd
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
            Date parse = simpleDateFormat.parse(source);
            return  parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
